package com.example.administrator.myapplication;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;

public class MainActivity extends AppCompatActivity {
    ArrayList<News> newsArrayList;
    // ListView
    ListView mLvNews;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    //初始化界面所需的数据
        initData();
        //初始化界面
        initView();
        //设置事件
        initEvent();

    }

    private void initEvent() {
        mLvNews.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Toast.makeText(MainActivity.this,""+position,Toast.LENGTH_LONG)
                        .show();
                //跳转到新的Avtivity,显示position位置的详细信息
                Intent intent = new Intent(MainActivity.this,DetailActivity.class);
                //携带数据
                intent.putExtra("position",position);
                intent.putExtra("icon",newsArrayList.get(position).getIcon());
                intent.putExtra("title",newsArrayList.get(position).getTitle());
                intent.putExtra("comment",newsArrayList.get(position).getComment());
                intent.putExtra("content",newsArrayList.get(position).getContent());
                intent.putExtra("type",newsArrayList.get(position).getType());

                //跳转
                startActivity(intent);
            }
        });
    }

    private void initView() {
        //找到列表控件
        mLvNews = findViewById(R.id.lv_news);
        //创建适配器对象
        MyBaseAdapter adapter = new MyBaseAdapter();
        //绑定适配器到ListView
        mLvNews.setAdapter(adapter);
    }

    private void initData(){
        // read String str from newsjson
        String str = readNewsJsonFile();
        // parse str to ArrayList<News>
        newsArrayList = parseJSON(str);
    }

    private ArrayList<News> parseJSON(String str) {
        //TODO 要动态解析json 字符串str
        //创建链表
        ArrayList<News> newsList = new ArrayList<>();
        try{
            JSONArray jsonArray = new JSONArray(str);
            for (int i=0;i < jsonArray.length();i++){
                JSONObject jsonObject = jsonArray.getJSONObject(i);
                //创建new对象
                News news = new News();

                news.setIcon(jsonObject.optString("icon"));
                news.setComment(jsonObject.optString("comment"));
                news.setTitle(jsonObject.optString("title"));
                news.setType(jsonObject.optString("type"));
                news.setContent(jsonObject.optString("content"));
                //将news添加到newsList中
                newsList.add(news);
            }
        } catch (JSONException e) {
            e.printStackTrace();
        }


        //创建new 对象
     //   News news = new News();
     //   news.setIcon("a.jpg");
     //   news.setComment("34");
     //   news.setTitle("搜狗输入");
     //   news.setType("1");
     //   news.setComment("搜狗输入法的简介");
     //   //将news添加到newsList中
     //   newsList.add(news);

        return newsList;
    }

    private String readNewsJsonFile() {
        String str = "";
        //构建输入流
        InputStream inputStream = getResources().openRawResource(R.raw.news);
        //读取数据
        try{
            //定义一个字节数组用来保存输入流中读取的信息
            //inputStream.available() 返回字节流中有效的字节数
            byte[] bytes = new byte[inputStream.available()];
            inputStream.read(bytes);
            inputStream.close();
            str = new String(bytes);
            return str;
        } catch (IOException e) {
            e.printStackTrace();
            return str;
        }
    }

    class MyBaseAdapter extends BaseAdapter{
        //ListView显示的数据项的数目
        @Override
        public int getCount() {
            return newsArrayList.size();
        }
        // position 参数是 数据项在ListView显示的位置
        @Override
        public Object getItem(int position) {
            return newsArrayList.get(position);
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            //从项布局创建View
            View view =  View.inflate(MainActivity.this
                    ,R.layout.list_item_layout,null);
            //从项目布局试图中获取图片控件
            ImageView icon = view.findViewById(R.id.iv_icon);
            //修改图片控件icon所显示的图片
            // TODO 需要动态的设置图标
            //获取当前数据项的图片名称 a.jpg/ b.jpg
            String pic = newsArrayList.get(position).getIcon();
            //根据文件名，获取资源Id
             int mipmap = getResources().getIdentifier(
                    pic.substring(0,pic.indexOf("."))
                    ,"mipmap"
                    ,getPackageName());


            icon.setImageResource(mipmap);
            //从项目布局视图中获取标题文本控件
            TextView tvTitle = view.findViewById(R.id.tv_title);
            //获取当前新闻标题，并在tvtitle中显示
            tvTitle.setText(newsArrayList.get(position).getTitle());
            //返回view
            return view;
        }
    }
    class News{
        private String icon;
        private String title;
        private String content;
        private String type;
        private String comment;

        News() {
        }

        public String getIcon() {
            return icon;
        }

        public void setIcon(String icon) {
            this.icon = icon;
        }

        public String getTitle() {
            return title;
        }

        public void setTitle(String title) {
            this.title = title;
        }

        public String getContent() {
            return content;
        }

        public void setContent(String content) {
            this.content = content;
        }

        public String getType() {
            return type;
        }

        public void setType(String type) {
            this.type = type;
        }

        public String getComment() {
            return comment;
        }

        public void setComment(String comment) {
            this.comment = comment;
        }


    }
}
